import { ApidiagnosisList, ApidoctorList  } from "../../../api/index"
import { formatDate, debounce } from "../../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: "a",
    offsetTop: 0,
    parmasA: {
      page: 1,
      size: 25,
      finish: false
    },
    parmasB: {
      page: 1,
      size: 10,
      finish: false
    },
    diagnosisList: [],
    doctorList: [],
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
    /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideTabBarRedDot({index:1})
    app.ws.onList(true,this.refreshList.bind(this))
    const activeTab = wx.getStorageSync("PAGEATOB")
    if(activeTab){
      this.setData({
        activeTab,
      },()=>{
        wx.removeStorageSync('PAGEATOB')
      })
    }
    this.getList2()
  },

  refreshList: debounce(function(data) {
    if(this.data.activeTab === "a") {
      const parmasA = {...this.data.parmasA}
      this.data.parmasA = {page: 1,size: this.data.doctorList.length}
      this.getDoctorList().finally(_ => {
        this.data.parmasA = parmasA
      })
    }
  },2000,false),

  goMessageRoom(e) {
    const item = e.currentTarget.dataset.item
    const ops = {
      icon: item.icon,
      doctor_id: item.doctor_id,
      doctor_name: item.doctor_name,
      department_name: item.department_name,
      position: item.position,
    }
    wx.navigateTo({
      url: '/pages/visit/message/index?contact=' + JSON.stringify(ops),
    })
  },

  goLink(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.link,
    })
  },

  onTabChange(e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.data.activeTab = e.detail.name
    if(this.data.activeTab === "a"){
      if(!this.data.doctorList.length){
        this.setData({
          loading: true
        })
      }
      this.getDoctorList()
    }else if(this.data.activeTab === "b") {
      if(!this.data.diagnosisList.length){
        this.setData({
          loading: true
        })
      }
      return this.getDiagnosisList()
    }
  },

  getList(flag) {
    if(this.data.activeTab === "a" && !this.data.parmasA.finish){
      this.data.parmasA.page = flag ? this.data.parmasA.page + 1 : 1
      this.setData({
        loading: true
      })
      return this.getDoctorList()
    }else if(this.data.activeTab === "b" && !this.data.parmasB.finish) {
      this.data.parmasB.page = flag ? this.data.parmasB.page + 1 : 1
      this.setData({
        loading: true
      })
      return this.getDiagnosisList()
    }
  },

  getList2() {
    if(this.data.activeTab === "a"){
      this.data.parmasA.page = 1
      if(!this.data.doctorList.length){
        this.setData({
          loading: true
        })
      }
      return this.getDoctorList()
    }else if(this.data.activeTab === "b") {
      this.data.parmasB.page = 1
      if(!this.data.diagnosisList.length){
        this.setData({
          loading: true
        })
      }
      return this.getDiagnosisList()
    }
  },

  getDoctorList() {
    return new Promise(resolve => {
      ApidoctorList(this.data.parmasA).then(res => {
        const data = res.data.map(item => {
          item.icon =  app.setPath(item.icon,true)
          item.last_time = formatDate(item.last_time) 
          if(item.type === 'img') {
            item.last_message = "[图片]"
          }
          return item
        })
        const doctorList = this.data.parmasA.page === 1 ? data : this.data.doctorList.concat(data)
        this.data.parmasA.finish = doctorList.length >= res.page.total
        this.setData({
          doctorList,
          loading: false
        })
      }).finally(_=>{
        resolve()
      })
    })
  },

  getDiagnosisList() {
    return new Promise(resolve => {
      ApidiagnosisList(this.data.parmasB).then(res => {
        const data = res.data.map(item => {
          item.expand = false
          return item
        })
        const diagnosisList = this.data.parmasB.page === 1 ? data : this.data.diagnosisList.concat(data)
        this.data.parmasB.finish = diagnosisList.length >= res.page.total
        this.setData({
          diagnosisList,
          loading: false
        })
      }).finally(_=>{
        resolve()
      })
    })
  },
  
  onExpand(e) {
    const index = e.currentTarget.dataset.index
    const expand = !this.data.diagnosisList[index].expand
    this.setData({
      [`diagnosisList[${index}].expand`]: expand
    })
  },

  getInfo(e) {
    this.setData({
      offsetTop: e.detail.bottom
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    app.ws.onList(false,()=>{})
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  onReachBottom() {
    if(this.data.loading) return
    this.getList(true)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})